package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.login.RequiresLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.atguigu.tingshu.vo.album.TrackStatVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album")
public class TrackInfoApiController {

    private final TrackInfoService trackInfoService;
    private final VodService vodService;

    public TrackInfoApiController(TrackInfoService trackInfoService, VodService vodService) {
        this.trackInfoService = trackInfoService;
        this.vodService = vodService;
    }


    @Operation(summary = "将音频文件上传到腾讯云点播平台")
    @Parameters({
            @Parameter(name = "file", description = "音频文件", required = true)
    })
    @PostMapping("/trackInfo/uploadTrack")
    public Result<Map<String, String>> uploadTrack(MultipartFile file) {
        return Result.ok(this.vodService.uploadTrack(file));
    }

    // /api/album/trackInfo/saveTrackInfo
    @Operation(summary = "保存声音信息")
    @Parameters({
            @Parameter(name = "trackInfoVo", description = "声音信息Vo对象", required = true)
    })
    @RequiresLogin
    @PostMapping("/trackInfo/saveTrackInfo")
    public Result<Void> saveTrackInfo(@RequestBody @Validated TrackInfoVo trackInfoVo) {
        Long userId = AuthContextHolder.getUserId();
        this.trackInfoService.saveTrackInfo(userId, trackInfoVo);
        return Result.ok();
    }

    @Operation(summary = "获取当前用户声音分页列表")
    @Parameters({
            @Parameter(name = "page", description = "当前页码", required = true),
            @Parameter(name = "limit", description = "每页记录数", required = true),
            @Parameter(name = "trackInfoQuery", description = "查询条件")
    })
    @RequiresLogin
    @PostMapping("/trackInfo/findUserTrackPage/{page}/{limit}")
    public Result<IPage<TrackListVo>> findUserTrackPage(@PathVariable Long page,
                                                        @PathVariable Long limit,
                                                        @RequestBody TrackInfoQuery trackInfoQuery) {
        Long userId = AuthContextHolder.getUserId();
        trackInfoQuery.setUserId(userId);
        Page<TrackListVo> trackListVoPage = new Page<>(page, limit);
        return Result.ok(this.trackInfoService.getUserTrackPage(trackListVoPage, trackInfoQuery));
    }

    @Operation(summary = "根据声音ID查询声音信息")
    @Parameters({
            @Parameter(name = "id", description = "声音ID", required = true)
    })
    @GetMapping("/trackInfo/getTrackInfo/{id}")
    public Result<TrackInfo> getTrackInfoById(@PathVariable Long id) {
        TrackInfo TrackInfo = this.trackInfoService.getById(id);
        return Result.ok(TrackInfo);
    }

    @Operation(summary = "修改声音信息")
    @Parameters({
            @Parameter(name = "id", description = "声音ID", required = true),
            @Parameter(name = "trackInfoVo", description = "声音信息Vo对象", required = true)
    })
    @RequiresLogin
    @PutMapping("/trackInfo/updateTrackInfo/{id}")
    public Result<Void> updateTrackInfo(@PathVariable Long id,
                                        @RequestBody @Validated TrackInfoVo trackInfoVo) {
        this.trackInfoService.updateTrackInfoById(id, trackInfoVo);
        return Result.ok();
    }

    @Operation(summary = "删除声音信息")
    @Parameters({
            @Parameter(name = "id", description = "声音ID", required = true)
    })
    @RequiresLogin
    @DeleteMapping("/trackInfo/removeTrackInfo/{id}")
    public Result<Void> removeTrackInfo(@PathVariable Long id) {
        this.trackInfoService.removeTrackInfoById(id);
        return Result.ok();
    }

    // /api/album/trackInfo/findAlbumTrackPage/{albumId}/{page}/{limit}
    @Operation(summary = "查询专辑声音分页列表")
    @Parameters({
            @Parameter(name = "albumId", description = "专辑ID", required = true),
            @Parameter(name = "page", description = "当前页码", required = true),
            @Parameter(name = "limit", description = "每页记录数", required = true)
    })
    @RequiresLogin(required = false)
    @GetMapping("/trackInfo/findAlbumTrackPage/{albumId}/{page}/{limit}")
    public Result<Page<AlbumTrackListVo>> findAlbumTrackPage(@PathVariable Long albumId,
                                                             @PathVariable Integer page,
                                                             @PathVariable Integer limit) {
        Long userId = AuthContextHolder.getUserId();
        Page<AlbumTrackListVo> trackPage = new Page<>(page, limit);
        return Result.ok(this.trackInfoService.getAlbumTrackPage(trackPage, albumId, userId));
    }

    @Operation(summary = "获取声音统计信息")
    @Parameters({
            @Parameter(name = "trackId", description = "声音ID", required = true)
    })
    @GetMapping("/trackInfo/getTrackStatVo/{trackId}")
    public Result<TrackStatVo> getTrackStatVo(@PathVariable Long trackId) {
        return Result.ok(this.trackInfoService.getTrackStatVoByTrackId(trackId));
    }

    @Operation(summary = "获取用户声音分集购买支付列表")
    @Parameters({
            @Parameter(name = "trackId", description = "声音ID", required = true)
    })
    @RequiresLogin
    @GetMapping("/trackInfo/findUserTrackPaidList/{trackId}")
    public Result<List<Map<String, Object>>> findUserTrackPaidList(@PathVariable Long trackId) {
        Long userId = AuthContextHolder.getUserId();
        List<Map<String, Object>> userTrackPaidList = this.trackInfoService.getUserTrackPaidList(trackId, userId);
        return Result.ok(userTrackPaidList);
    }

    @Operation(summary = "根据声音ID+声音数量 获取下单付费声音列表")
    @Parameters({
            @Parameter(name = "trackId", description = "声音ID", required = true),
            @Parameter(name = "trackCount", description = "声音数量", required = true)
    })
    @RequiresLogin
    @GetMapping("/trackInfo/findPaidTrackInfoList/{trackId}/{trackCount}")
    public Result<List<TrackInfo>> findPaidTrackInfoList(@PathVariable Long trackId,
                                                         @PathVariable Integer trackCount) {
        List<TrackInfo> paidTrackInfoList = this.trackInfoService.getPaidTrackInfoList(trackId, trackCount);
        return Result.ok(paidTrackInfoList);
    }

}

